<?php

class User_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    /* Lire les teams (pour formulaire ajout d'un joueur)  */
    public function lireTeams() {

        $result = $this->db->get('teams');

        return $result->result();
    }
    
    //Renvoi la liste des jeux
    public function lireGames()
    {
    	$result = $this->db->get('games');
    	
    	return $result->result();
    }

    public function ajouter(Userclass $unUser) {
        $this->db->insert("users", $unUser);
    	
    	return $this->db->_error_message();
    }
    
    
    public function checkLogin($login, $password) {
        $this->db->where('login', $login);
        $this->db->where('password', $password);
        $result = $this->db->get('users');
        return $result->result();
    }
    
    //Renvoi si l'user est admin
    public function check_admin()
    {
    	$isAdmin = true;
    	
    	$user = $this->session->all_userdata();
    	if ($user['level'] == 1)
    	{
    		$isAdmin = false;
    	}
    	
    	return $isAdmin;    	
    }
    
    
    // Renvoi l'id du user connecte
    public function check_id()
    {
    	$sess = $this->session->all_userdata();
    	return $sess['id_user'];
    }
    
    
    // Renvoi tous les users de la base
    public function readAll()
    {
    	$query = $this->db->get('users');
    	
    	return $query->result();
    	
    }
    
    //Retourne que les utilisateurs (sans les admins)
    public function readAllUser()
    {
    	$this->db->where("level", "1");

    	$result = $this->db->get('users');
    	
    	return $result->result();
    }
}
